package com.gukun.springboot.tdengine.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gukun.springboot.tdengine.domain.backuprecover.BackupRecoverLogEntity;
import com.gukun.springboot.tdengine.domain.backuprecover.BackupRecoverTaskEntity;
import com.gukun.springboot.tdengine.domain.common.CommonResult;
import com.gukun.springboot.tdengine.service.backuprecover.BackupRecoverLogService;
import com.gukun.springboot.tdengine.service.backuprecover.BackupRecoverTaskService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author zhang
 */
@RequestMapping("/api/tdengine/backupRecover")
@RestController
public class BackupRecoverTaskController {
    @Autowired
    private BackupRecoverTaskService backupRecoverTaskService;
    @Autowired
    private BackupRecoverLogService backupRecoverLogService;

    @Operation(summary = "保存更新备份任务", description = "保存更新备份任务")
    @PostMapping("/saveOrUpdateBackupTask")
    public CommonResult<Boolean> saveOrUpdateBackupTask(@RequestBody BackupRecoverTaskEntity backupTask){
        return CommonResult.success(backupRecoverTaskService.saveOrUpdateBackupTask(backupTask));
    }

    @Operation(summary = "获取备份恢复任务列表", description = "获取备份恢复任务列表")
    @PostMapping("/getBackupAndRecoverTask")
    public CommonResult<Page<BackupRecoverTaskEntity>> getBackupAndRecoverTask(@RequestBody BackupRecoverTaskEntity taskEntity) throws Exception {
        return CommonResult.success(backupRecoverTaskService.getBackupAndRecoverTask(taskEntity));
    }

    @Operation(summary = "启动任务", description = "启动任务")
    @PostMapping("/startTask")
    public CommonResult<Boolean> startTask(@RequestBody BackupRecoverTaskEntity backupRecoverTaskEntity) throws Exception {
        return CommonResult.success(backupRecoverTaskService.startTask(backupRecoverTaskEntity));
    }
    @Operation(summary = "查询任务日志", description = "根据任务ID查询任务日志")
    @PostMapping("/getTaskLog")
    public CommonResult<Page<BackupRecoverLogEntity>> getTaskLog(@RequestBody BackupRecoverLogEntity backupRecoverLogEntity){
        return CommonResult.success(backupRecoverLogService.getLogByTaskId(backupRecoverLogEntity));
    }

    @Operation(summary = "删除备份任务", description = "根据任务ID删除备份任务")
    @GetMapping("/deleteTask")
    public CommonResult<Boolean> deleteTask(@RequestParam String id){
        return CommonResult.success(backupRecoverTaskService.deleteTask(id));
    }

    @Operation(summary = "停止任务", description = "停止任务")
    @GetMapping("/stopTask")
    public CommonResult<Boolean> stopTask(@RequestParam String id) throws Exception {
        return CommonResult.success(backupRecoverTaskService.stopTask(id));
    }

    /**
     * 下载备份的测点数据
     * @return 结果
     */
    @Operation(summary = "下载备份的测点数据", description = "下载备份的测点数据")
    @GetMapping("/downloadTagInfo")
    public void downloadTagInfo(HttpServletResponse response, @RequestParam String id) throws Exception {
        backupRecoverTaskService.downloadTagInfo(id,response);
    }

    @Operation(summary = "获取任务日志", description = "获取正在运行的任务的最新日志")
    @GetMapping("/getRunningTaskLog")
    public CommonResult<BackupRecoverLogEntity> getRunningTaskLog(@RequestParam String id) throws Exception {
        return CommonResult.success(backupRecoverLogService.getRunningLogByTaskId(id));
    }

    @Operation(summary = "获取任务", description = "获取正在运行的任务")
    @GetMapping("/getRunningTask")
    public CommonResult<BackupRecoverTaskEntity> getRunningTask(@RequestParam String id) throws Exception {
        return CommonResult.success(backupRecoverTaskService.getById(id));
    }

}
